package demo02;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
    HashMap存储自定义类型键值
    Map集合保证key是唯一的:
        作为key的元素,必须重写hashCode方法和equals方法,以保证key唯一
 */
public class Demo01HashMapSavePerson {
    public static void main(String[] args) {
        method01();
        method02();

    }

    private static void method02() {
        HashMap<Person,String>hashMap=new HashMap<>();
        hashMap.put(new Person("张三",17),"东北石油大学");
        hashMap.put(new Person("李四",21),"黑龙江八一农垦大学");
        hashMap.put(new Person("李四",21),"黑龙江八一农垦大学");
        hashMap.put(new Person("王五",19),"哈尔滨工业大学");
        Set<Map.Entry<Person, String>> entries = hashMap.entrySet();
        entries.forEach(personStringEntry -> System.out.println(personStringEntry));

    }

    /*
        HashMap存储自定义类型键值
        key:String类型
            String类重写hashCode方法和equals方法,可以保证key唯一
        value:Person类型
            value可以重复(同名同年龄的人视为同一个)
     */
    private static void method01() {
        HashMap<String,Person> HashMap=new HashMap<>();
        HashMap.put("北京",new Person("张三",18));
        HashMap.put("上海",new Person("李四",20));
        HashMap.put("深圳",new Person("王五",19));
        Set<String> keySet = HashMap.keySet();
        for (String s : keySet) {
            Person person = HashMap.get(s);
            System.out.println(s+"-->"+person);
        }

    }
}
